home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / canvas.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  7KB  |  308 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "const.h"
  13. #include "paintop.h"
  14.  
  15. /***************** import global variables and routines *******************/
  16.  
  17. extern int        cur_command;
  18. extern int        errno;
  19. extern int        action_on;     /* tracker.c */
  20. extern int        magnet_mode;
  21.  
  22. extern int        receiving_msg;
  23. extern int        borderwid, stripeht, windowspacing;
  24. extern int        CANVAS_WIDTH, CANVAS_HEIGHT;
  25. extern            null_proc();
  26. extern            (*canvas_kbd_proc)();
  27. extern            (*canvas_locmove_proc)();
  28. extern            (*canvas_leftbut_proc)();
  29. extern            (*canvas_middlebut_proc)();
  30. extern            (*canvas_rightbut_proc)();
  31. extern            null_proc();
  32. extern            set_popupmenu();
  33.  
  34.  
  35. /*********************** local variables and routines ************************/
  36.  
  37. static            canvas_selected();
  38. static            canvas_sighandler();
  39.  
  40. #ifndef    X11
  41. #ifdef AMIGA
  42. #else
  43. int
  44. init_canvas(tool)
  45. TOOL    tool;
  46. {
  47.     INPUTMASK     mask;
  48.  
  49.     canvas_sw = tool_createsubwindow(tool, "", CANVAS_WIDTH, CANVAS_HEIGHT);
  50.     if (canvas_sw == (TOOLSW)0) return(0);
  51.     canvas_swfd = canvas_sw->ts_windowfd;
  52.  
  53.     canvas_pixwin = pw_open(canvas_swfd);
  54.     canvas_pixwin->pw_prretained = mem_create(CANVAS_WIDTH, 
  55.                         CANVAS_HEIGHT, 1);
  56.     canvas_sw->ts_io.tio_selected = canvas_selected;
  57.     canvas_sw->ts_io.tio_handlesigwinch = canvas_sighandler;
  58.     canvas_sw->ts_destroy = null_proc;
  59.  
  60.     input_imnull(&mask);
  61.     win_setinputcodebit(&mask, MS_LEFT);
  62.     win_setinputcodebit(&mask, MS_MIDDLE);
  63.     win_setinputcodebit(&mask, MS_RIGHT);
  64.     win_setinputcodebit(&mask, LOC_MOVE);
  65.     mask.im_flags |= IM_ASCII | IM_INTRANSIT;
  66.     win_setinputmask(canvas_swfd, &mask, NULL, WIN_NULLLINK);
  67.     set_temp_cursor(cur_cursor);
  68.     (void)fcntl(canvas_swfd, F_SETFL, fcntl(canvas_swfd, F_GETFL, 0) | O_NDELAY);
  69.     init_grid();
  70.     canvas_leftbut_proc = null_proc;
  71.     canvas_middlebut_proc = null_proc;
  72.     canvas_rightbut_proc = null_proc;
  73.     canvas_kbd_proc = canvas_locmove_proc = null_proc;
  74.     return(1);
  75.     }
  76.  
  77. static
  78. canvas_selected(nullsw, ibits, obits, ebits, timer)
  79. caddr_t        *nullsw;
  80. int        *ibits, *obits, *ebits;
  81. struct timeval    *timer;
  82. {
  83.     static INPUTEVENT        ie;
  84.     static int            x, y, t;
  85.  
  86.     *ibits = *obits = *ebits = 0;
  87.     if (input_readevent(canvas_swfd, &ie) == -1) {
  88.         printf ("error %d in canvas_selected", errno);
  89.         return;
  90.         }
  91.  
  92.     x = ie.ie_locx;
  93.     y = ie.ie_locy;
  94.     while (ie.ie_code == LOC_MOVE) {
  95.         if (input_readevent(canvas_swfd, &ie) == -1) {
  96.         if (magnet_mode) {
  97.             x = ((t = x % 5) < 3) ? x - t - 1 : x + 5 - t - 1;
  98.             y = ((t = y % 5) < 3) ? y - t - 1 : y + 5 - t - 1;
  99. /*
  100.             win_setmouseposition(canvas_swfd, x, y);
  101. */
  102.             }
  103.         set_rulermark(x, y);
  104.         (*canvas_locmove_proc)(x, y);
  105.         return;
  106.         }
  107.         x = ie.ie_locx;
  108.         y = ie.ie_locy;
  109.         }
  110.     if (magnet_mode) {
  111.         x = ((t = x % 5) < 3) ? x - t - 1 : x + 5 - t - 1;
  112.         y = ((t = y % 5) < 3) ? y - t - 1 : y + 5 - t - 1;
  113. /*
  114.         win_setmouseposition(canvas_swfd, x, y);
  115. */
  116.         }
  117.     if (ie.ie_code == MS_LEFT) {
  118.         canvas_leftbut_proc(x, y);
  119.         }
  120.     else if (ie.ie_code == MS_RIGHT) {
  121.         canvas_rightbut_proc(&ie);
  122.         }
  123.     else if (ie.ie_code == MS_MIDDLE) {
  124.         canvas_middlebut_proc(x, y);
  125.         }
  126.     else if (ie.ie_code <= ASCII_LAST) {
  127.         canvas_kbd_proc(ie.ie_code);
  128.         }
  129.     }
  130.  
  131. static
  132. canvas_sighandler()
  133. {
  134.     pw_damaged(canvas_pixwin);
  135.     pw_repairretained(canvas_pixwin);
  136.     pw_donedamaged(canvas_pixwin);
  137.     }
  138.  
  139. clear_canvas()
  140. {
  141.     pw_writebackground(canvas_pixwin, 0, 0, 2048, 2048, PAINT);
  142.     }
  143. #endif AMIGA
  144. #else
  145. static Arg      canvas_args[] =
  146. {
  147.     { XtNx, (XtArgVal)0 },
  148.     { XtNy, (XtArgVal)0 },
  149.     { XtNlabel, (XtArgVal)"" },
  150.     { XtNwidth, (XtArgVal)0 },
  151.     { XtNheight, (XtArgVal)0 },
  152.     { XtNfromHoriz, (XtArgVal)NULL },
  153.     { XtNhorizDistance, (XtArgVal)0 },
  154.     { XtNfromVert, (XtArgVal)NULL },
  155.     { XtNvertDistance, (XtArgVal)0 },
  156.     { XtNtop, (XtArgVal)XtChainTop },
  157. };
  158.  
  159. static void canvas_exposed(tool, event, params, nparams)
  160.     TOOL        tool;
  161.     INPUTEVENT    *event;
  162.     String        *params;
  163.     Cardinal    *nparams;
  164. {
  165.     if (((XExposeEvent *)event)->count > 0)
  166.         return;
  167.     redisplay_canvas();
  168.     redisplay_rulers();
  169. }
  170.  
  171. static void set_pos(tool, event, params, nparams)
  172.     TOOL        tool;
  173.     XEvent        *event;
  174.     String        *params;
  175.     Cardinal    *nparams;
  176. {
  177.     Position    x, y;
  178.     Window        w;
  179.     extern TOOL    menu;
  180.     
  181.     XTranslateCoordinates(tool_d, canvas_swfd, XDefaultRootWindow(tool_d),
  182.                   event->xbutton.x, event->xbutton.y, &x, &y, &w);
  183.     XtMoveWidget(menu, x-10, y-10);
  184. }
  185.  
  186. XtActionsRec canvas_actions[] =
  187. {
  188.     { "Event", (XtActionProc)canvas_selected },
  189.     { "Expose", (XtActionProc)canvas_exposed },
  190.     { "set_pos", (XtActionProc)set_pos },
  191. };
  192.  
  193. static String canvas_translations =
  194.     "<Motion>:Event()\n\
  195.     <Btn1Down>:Event()\n\
  196.     <Btn2Down>:Event()\n\
  197.     <Btn3Down>:set_pos(popup_menu)MenuPopup(popup_menu)\n\
  198.     <Key>:Event()\n\
  199.     <Expose>:Expose()\n";
  200.  
  201. static    Arg    canvas_color_args[] = {
  202.     { XtNforeground, (XtArgVal) &x_fg_color.pixel },
  203.     { XtNbackground, (XtArgVal) &x_bg_color.pixel },
  204. };
  205.  
  206. int init_canvas(tool)
  207.     TOOL        tool;
  208. {
  209.     XColor    fixcolors[2];
  210.     
  211.     canvas_args[3].value = CANVAS_WIDTH;
  212.     canvas_args[4].value = CANVAS_HEIGHT;
  213.     canvas_args[5].value = (XtArgVal)panel_sw;
  214.     canvas_args[7].value = (XtArgVal)topruler_sw;
  215.     canvas_sw = XtCreateWidget("canvas", labelWidgetClass, tool,
  216.         canvas_args, XtNumber(canvas_args));
  217.     XtGetValues(canvas_sw, canvas_color_args, XtNumber(canvas_color_args));
  218.  
  219.     /* get the RGB values for recolor cursor use -- may want to have
  220.        cursor color resource */
  221.     fixcolors[0] = x_fg_color;
  222.     fixcolors[1] = x_bg_color;
  223.     XQueryColors(tool_d, DefaultColormapOfScreen(tool_s), fixcolors, 2);
  224.     x_fg_color = fixcolors[0];
  225.     x_bg_color = fixcolors[1];
  226.     
  227.     /* now fix the global GC */
  228.     XSetState(tool_d, gc, x_fg_color.pixel, x_bg_color.pixel, GXcopy,
  229.           AllPlanes);
  230.     
  231.     /* and recolor the cursors */
  232.     recolor_cursors();
  233.     
  234.     canvas_leftbut_proc = null_proc;
  235.     canvas_middlebut_proc = null_proc;
  236.     canvas_rightbut_proc = null_proc;
  237.     canvas_kbd_proc = canvas_locmove_proc = null_proc;
  238.     XtAddActions(canvas_actions, XtNumber(canvas_actions));
  239.     XtOverrideTranslations(canvas_sw,
  240.         XtParseTranslationTable(canvas_translations));
  241.     return (1);
  242. }
  243.  
  244. setup_canvas()
  245. {
  246.     canvas_rightbut_proc = set_popupmenu;
  247.     canvas_pixwin = canvas_swfd = XtWindow(canvas_sw);
  248.     init_grid();
  249. }
  250.  
  251. static canvas_selected(tool, event, params, nparams)
  252.     TOOL        tool;
  253.     INPUTEVENT    *event;
  254.     String        *params;
  255.     Cardinal    *nparams;
  256. {
  257.     register int    x, y, t;
  258.     char        buf[1];
  259.     XButtonPressedEvent    *be = (XButtonPressedEvent *)event;
  260.     XKeyPressedEvent    *ke = (XKeyPressedEvent *)event;
  261.  
  262.     x = event->x;
  263.     y = event->y;
  264.     if (magnet_mode)
  265.     {
  266.         x = ((t = x % 5) < 3) ? x - t - 1 : x + 5 - t - 1;
  267.         y = ((t = y % 5) < 3) ? y - t - 1 : y + 5 - t - 1;
  268.     }
  269.     switch (event->type)
  270.     {
  271.     case MotionNotify:
  272.         {
  273.             Window rw, cw;
  274.             static int sx = -10000, sy = -10000;
  275.             int rx, ry, cx, cy;
  276.             unsigned int mask;
  277.  
  278.             XQueryPointer(event->display, event->window,
  279.                       &rw, &cw,
  280.                       &rx, &ry,
  281.                       &cx, &cy,
  282.                       &mask);
  283.             if(cx == sx && cy == sy) break;
  284.             x = sx = cx;
  285.             y = sy = cy;
  286.         }
  287.         set_rulermark(x, y);
  288.         (*canvas_locmove_proc)(x, y);
  289.         break;
  290.     case ButtonPress:
  291.         if (be->button & Button1)
  292.             (*canvas_leftbut_proc)(x, y);
  293.         else if (be->button & Button3)
  294.             (*canvas_middlebut_proc)(x, y);
  295.         break;
  296.     case KeyPress:
  297.         if (XLookupString(ke, buf, sizeof(buf), NULL, NULL) > 0)
  298.             (*canvas_kbd_proc)(buf[0]);
  299.         break;
  300.     }
  301. }
  302.  
  303. clear_canvas()
  304. {
  305.     XClearArea(tool_d, canvas_pixwin, 0, 0, 0, 0, False);
  306. }
  307. #endif    X11
  308.